(function(window) {
    var Subject_tikuCtrl = {
        data: {},
        init: function(data) {
            this.bindEven();
            var w = $(window).width();
            if (view_type == 'Web') {
                var h = $(window).height();
                video_h = h - 60;
               // alert(w);
                var max_w=w*0.666666-30;
                $(".action_box").height(video_h -1);
                $("#videoWrapper").height(video_h);
                $("#videoWrapper").width(max_w);
                 $("#videoWrapper video").width(max_w);
               //$("#videoWrapper video").height(video_h);
            } else { //手机
              Subject_tikuCtrl.orientationchange();
               //屏幕翻转
               window.addEventListener("resize", function() {
                Subject_tikuCtrl.orientationchange();
               }, false);
            }
            //	hiApp.bind($('[data-page="Subject_tiku"]'));
            
            Subject_tikuCtrl.videoPlayer();
            //Subject_tikuCtrl.questionHis();
            $(".toolbar").hide();
           
        },
        orientationchange:function(){
        	  var w = $(window).width();
        	  var video_h = w * 0.5625;
        	  var max_h=$(window).height();
        	  //alert($(window).height());
        	  $(".pages").height($(window).height());
        	  $(".action_box").parents('.page').height($(window).height());
        	  $(".action_box").parents('.page-content').scrollTop(0);
        	  //alert(max_h);
        	  $(".action_box").parents('.page-content').css({"padding-bottom":"0px"});
        	  if(max_h<video_h){
        	  	video_h=max_h;
        	  	//alert(12);
        	  	$(".action_box").parents('.page-content').css({"overflow-y":"hidden"});
        	  }else{
        	  	$(".action_box").parents('.page-content').css({"overflow-y":"hidden"});
        	  	//$("#action_box").attr("init",0);
                //hiApp.swiperPage("#action_box");
        	  }
        	  var max_w=$(window).width();
        	  $("#action_box").attr("h",max_h- video_h-50);
              $("#videoWrapper").width(max_w);
              $("#videoWrapper").height(video_h);
              $("#videoWrapper video").height(video_h);
              $("#videoWrapper video").width(max_w);

        },
        afterShow: function() {
            $(".toolbar").hide();
            $("#header").hide();
        },
        afterHide:function(){
         $("#header").show();
          CKobject.getObjectById('ckplayer_videoWrapper').videoPause();
          $(".toolbar").show();
        },
        //显示做题记录
        questionHis: function() {
            $(".all_questions").each(function() {
                if ($(this).find("dd .checked").length > 0) {
                    $(this).find(".sumbit_answer").trigger(hiApp.device.click);
                }
            })
        },


        bindEven: function() {
            var bindings = [{
                // element:document,
                element: '.learnchapter h3',
                event: hiApp.device.click,
                handler: this.show_lesson
            }, {
                element: '.sumbit_answer',
                event: hiApp.device.click,
                handler: this.post_answer
            }, {
                element: '.questions_next',
                event: hiApp.device.click,
                handler: this.questions_next
            }];
            bindEvents(bindings);
        },
        videoPlayer: function() {
            if ($("#videoWrapper").length == 0) return;
            console.log(video_info);
            if (video_info == null) {
                video_info = {};
            }
            var w=$(window).width();
            if(w>500){
                play_url = video_info.m3u8_720;
            }else{
                 play_url = video_info.m3u8_480;
                 // play_url = video_info.m3u8_720;
            }
           

            if (typeof(play_url) != "string") {
                play_url = 'http://movie.ks.js.cn/flv/other/1_0.mp4';
            }
            var param = {
                id: 'videoWrapper',
                url: play_url,
                image: video_info.thumbnail,
                //	loaded:"loadedHandler('videoPlayer')",//【初始化调到第几秒
            };
            hiApp.playVideo(param);
        },

        questions_next: function() {
            var _this = $(".all_questions:visible");
            var l = _this.next(".all_questions").length;
            if (l == 0) {
                dialog("恭喜，您已经完所有练习");

            } else {
                _this.hide();
                _this.next(".all_questions").show();
            }
        },

        post_answer: function() {
            var data_id = $(this).attr("data-id");
            var answer_remark = $('[data-id="all_questions_' + data_id + '"]').find(".answer_remark")
            answer_remark.show();
            var anwser = $("#hideQuestionanswer_" + data_id).val();
            var post_anwser = "";
            $('[data-id="all_questions_' + data_id + '"]').find("dd .checked").each(function(i, v) {
                post_anwser += $(v).attr("key");
            });
            if (post_anwser == anwser) {
                var remark = '<span class="glyphicon glyphicon-ok-circle"></span>';
            } else {
                var remark = '<span class="glyphicon glyphicon-remove-circle"></span>';
            }
            //$('[data-id="all_questions_'+data_id+'"]').find("dd").unbind(hiApp.device.click);
            answer_remark.html(remark);
            $("#questions_answer_" + data_id).show();
            $(this).hide();
        },

        show_lesson: function() {
            CM.init("BoxEdu", "Subject_lessonCtrl").show_lesson($(this))
        },


    };

    window.BoxEdu_Subject_tikuCtrl = Subject_tikuCtrl;

})(window);
